
goog.provide('esn.FriendSurvey');

goog.require('esn.Slider');
goog.require('esn.dao.FriendSurveyDao');
goog.require('esn.soy.friendSurveyList');
goog.require('goog.array');
goog.require('goog.dom.classes');
goog.require('goog.style');
goog.require('goog.ui.Component');
goog.require('goog.ui.CustomButton');


/**
 * @constructor
 */
esn.FriendSurvey = function(appContext, friendSurveyModel, opt_domHelper) {
  goog.base(this, opt_domHelper);

  this.friendSurveyDao_ = new esn.dao.FriendSurveyDao(
      friendSurveyModel,
      appContext.getService(esn.constants.ServiceId.SURVEY_CLIENT),
      2000);

  this.setId(String(friendSurveyModel.getFriend().getId()));
  this.setModel(friendSurveyModel);
};
goog.inherits(esn.FriendSurvey, goog.ui.Component);

esn.FriendSurvey.EventType = {
  REMOVE_CLICK: goog.events.getUniqueId('remove_click')
};

esn.FriendSurvey.MAX_SLIDER_VALUE = 100;

esn.FriendSurvey.prototype.getDao = function() {
  return this.friendSurveyDao_;
};

esn.FriendSurvey.prototype.createDom = function() {  
  var dom = this.getDomHelper();
  var tableEl = dom.createElement('table');
  tableEl.innerHTML = esn.soy.friendSurveyList.row(
      {'Friend': this.getModel().getFriend().toObject()});
  var rowEl = tableEl.getElementsByTagName('tr')[0];
  this.setElementInternal(rowEl);

  this.removeButton_ = new goog.ui.CustomButton('Remove');
  this.addChild(this.removeButton_);
  this.removeButton_.render(this.getElementByClass('removeButton'));
};

esn.FriendSurvey.prototype.enterDocument = function() {  
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this,
      esn.Slider.EventType.CHANGE,
      this.handleSliderChange_);

  this.getHandler().listen(
      this.getElement(),
      goog.events.EventType.CHANGE,
      this.handleRadioChange_);

  this.getHandler().listen(
      this.removeButton_,
      goog.ui.Component.EventType.ACTION,
      function(e) {
        this.dispatchEvent(esn.FriendSurvey.EventType.REMOVE_CLICK);
      });
};

esn.FriendSurvey.prototype.validateCompletion = function() {
  if (!goog.isDef(this.getModel().getFriendshipLevel())) {
    var cellEl = this.friendshipLevelSlider_.getElement().parentNode;
    goog.dom.classes.add(cellEl, 'missing');
  }
  if (!goog.isDef(this.getModel().getInteractionLevel())) {
    var cellEl = this.interactionSlider_.getElement().parentNode;
    goog.dom.classes.add(cellEl, 'missing');
  }
  if (!goog.isDef(this.getModel().getAskAdviceLevel())) {
    var cellEl = this.askAdviceSlider_.getElement().parentNode;
    goog.dom.classes.add(cellEl, 'missing');
  }
};

esn.FriendSurvey.prototype.handleSliderChange_ = function(e) {
  if (e.target instanceof esn.Slider) {
    var slider = e.target;
    if (slider == this.friendshipLevelSlider_) {
      this.getModel().setFriendshipLevel(
          this.friendshipLevelSlider_.getValue());
    } else if (slider == this.interactionSlider_) {
      this.getModel().setInteractionLevel(this.interactionSlider_.getValue());
    } else {
      this.getModel().setAskAdviceLevel(this.askAdviceSlider_.getValue());
    }
    // Save changes.
    this.friendSurveyDao_.save();    
  }
};
